package com.gugee.tiktok.data.consumer.blogger.core.spider;

import com.gugee.tiktok.common.model.spider.blogger.BloggerFollowerResponse;
import com.gugee.tiktok.common.model.spider.blogger.BloggerFollowingResponse;
import com.gugee.tiktok.common.model.spider.blogger.BloggerResponse;

import com.xiguaji.boot.retry.RetryableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

@Component
public class BloggerSpiderClient {
    @Autowired
    RestTemplate restTemplate;

    @Value("${xiguaji.spider.host}")
    String host;

    @Retryable(value = RetryableException.class, maxAttemptsExpression = "${spider.blogger.retry:5}")
    public BloggerResponse get(Long uid, String region) {
        String uri = host + "/aweme/UserProfileFromUid?user_id={user_id}&region={region}";
        if (StringUtils.isEmpty(region)) {
            region = "US";
        }

        ResponseEntity<BloggerResponse> responseEntity = restTemplate.getForEntity(uri, BloggerResponse.class, uid, region);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get blogger error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }
        if (responseEntity.getBody() == null || responseEntity.getBody().getStatus_code() == 3071) {

            throw new RetryableException("get blogger error, retry times 10. uri:" +
                    uri.replace("{user_id}", uid.toString()).replace("{region}", region));
        }
        return responseEntity.getBody();
    }

    @Retryable(value = RetryableException.class, maxAttemptsExpression = "${spider.blogger.following.retry:5}")
    public BloggerFollowingResponse getFollowings(Long uid, long min_time, int count) {
        String uri = host + "/aweme/FollowingListSimple?user_id={user_id}&sec_user_id=&max_time={max_time}&count={count}&region=US";
        ResponseEntity<BloggerFollowingResponse> responseEntity = restTemplate.getForEntity(uri, BloggerFollowingResponse.class, uid, min_time, count);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get bloggerFollow error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }
        if (responseEntity.getBody() == null || responseEntity.getBody().getStatus_code() == 3071) {
            throw new RetryableException("get blogger followings error, retry times 10. uri:" + uri + " min_time:" + min_time + " count:" + count);
        }

        return responseEntity.getBody();
    }

    @Retryable(value = RetryableException.class, maxAttemptsExpression = "${spider.blogger.follower.retry:5}")
    public BloggerFollowerResponse getFollowers(long uid, long min_time, int count) {
        String uri = host + "/aweme/FollowerListSimple?user_id={user_id}&max_time={max_time}&count={count}&region=US";
        ResponseEntity<BloggerFollowerResponse> responseEntity = restTemplate.getForEntity(uri, BloggerFollowerResponse.class, uid, min_time, count);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get blogger follower error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }
        if (responseEntity.getBody() == null || responseEntity.getBody().getStatus_code() == 3071) {
            throw new RetryableException("get blogger follower error, retry times 10. uri:" + uri + " max_time:" + min_time + " count:" + count);
        }
        return responseEntity.getBody();
    }
}
